package com.cargohu.aishouyu.file.controller;

import com.cargohu.aishouyu.file.dto.FileUpdateDTO;
import com.cargohu.aishouyu.file.req.FileRequest;
import com.cargohu.aishouyu.file.resp.FileDetailVo;
import com.cargohu.aishouyu.file.resp.FileResponse;
import com.cargohu.aishouyu.file.service.FileService;
import com.cargohu.aishouyu.framework.page.Paging;
import com.cargohu.aishouyu.framework.response.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * 文件管理表 控制器
 *
 * @author aaron.wang
 * @since 2024-01-07
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Tag(name = "文件管理表")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 添加文件管理表
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/upload-file")
    @Operation(summary = "上传文件")
    public ApiResult addFile(@RequestPart("file") MultipartFile file) throws Exception {
        Map<String, String> result = fileService.uploadFile(file);
        return ApiResult.success(result);

    }

    /**
     * 获取文件
     *
     * @param bucket   桶名称
     * @param fileName 文件空间/名称
     * @param response
     * @return void
     */
    @GetMapping("/{bucket}/{fileName}")
    public void file(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response) {
        fileService.getFile(bucket, fileName, response);
    }
    /**
     * 修改文件管理表
     *
     * @param fileUpdateDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/update-file")
    @Operation(summary = "修改文件管理表")
    public ApiResult updateFile(@Valid @RequestBody FileUpdateDTO fileUpdateDTO) throws Exception {
        boolean flag = fileService.updateFile(fileUpdateDTO);
        return ApiResult.result(flag);

    }


    /**
     * 删除文件管理表
     *
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete-file/{id}")
    @Operation(summary = "删除文件管理表")
    public ApiResult deleteFile(@PathVariable Long id) throws Exception {
        boolean flag = fileService.deleteFile(id);
        return ApiResult.result(flag);

    }

    /**
     * 获取文件管理表详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/file-info/{id}")
    @Operation(summary = "文件管理表详情")
    public ApiResult<FileDetailVo> getFile(@PathVariable Long id) throws Exception {
        FileDetailVo fileDetailVo = fileService.getFileById(id);
        return ApiResult.success(fileDetailVo);

    }

    /**
     * 文件管理表分页列表
     *
     * @param fileRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/file-page")
    @Operation(summary = "文件管理表分页列表")
    public ApiResult<FileResponse> getFileList(@Valid @RequestBody FileRequest fileRequest) throws Exception {
        Paging<FileResponse> paging = fileService.getFileList(fileRequest);
        return ApiResult.success(paging);
    }

}
